package cn.itcast.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("hello")//相当于多一个二级路径
public class Hello2Controller {

	/**
	 * 映射url请求和方法
	 * @return
	 */
	@RequestMapping("show1")
	public ModelAndView test1(){
      
		ModelAndView mv = new ModelAndView();
		mv.setViewName("hello");
		mv.addObject("msg", "这是第一个springmvc的注解程序！");
		return mv;
	}
	
	/**
	 * ant风格的映射：
	 * 	1、？：表示匹配任意单个字符
	 * 	2、*：表示匹配任意多个字符，0个、1个、多个
	 * 	3、**：表示任意多级路径
	 */
	@RequestMapping("*/show2")
	public ModelAndView test2(){
		ModelAndView mv = new ModelAndView();
		mv.setViewName("hello");
		mv.addObject("msg", "这是ant风格的映射方式");
		return mv;
	}
	
	@RequestMapping("**/show3")
	public ModelAndView test3(){
		ModelAndView mv = new ModelAndView();
		mv.setViewName("hello");
		mv.addObject("msg", "这是ant风格的映射方式:**，表示任意多级路径");
		return mv;
	}
	@RequestMapping("show4/{name}/{id}")
	public ModelAndView test4(@PathVariable("name")String name,@PathVariable("id")Long id){
		ModelAndView mv = new ModelAndView();
		mv.setViewName("hello");
		mv.addObject("msg", "占位符的映射方式:"+name+"..."+id);
		return mv;
	}
	/**
	 * method=RequestMethod.POST:只有post请求能映射到这个方法
	 * @param
	 * @param
	 * @return
	 */
	@RequestMapping(value="show5",method=RequestMethod.POST)
	public ModelAndView test5(){
		System.out.println();
		ModelAndView mv = new ModelAndView();
		mv.setViewName("hello");
		mv.addObject("msg", "限定请求方法的映射：Post");
		return mv;
	}
	@RequestMapping(value="show6",method={RequestMethod.POST,RequestMethod.GET})
	public ModelAndView test6(){
		ModelAndView mv = new ModelAndView();
		mv.setViewName("hello");
		mv.addObject("msg", "限定请求方法的映射：Post和Get");
		return mv;
	}
	@RequestMapping(value="show7",params="userId")
	public ModelAndView test7(){
		ModelAndView mv = new ModelAndView();
		mv.setViewName("hello");
		mv.addObject("msg", "限定请求参数的映射：参数必须包含userId");
		return mv;
	}
	@RequestMapping(value="show8",params="!userId")
	public ModelAndView test8(){
		ModelAndView mv = new ModelAndView();
		mv.setViewName("hello");
		mv.addObject("msg", "限定请求参数的映射：参数必须不包含userId");
		return mv;
	}
	@RequestMapping(value="show9",params="userId=101")
	public ModelAndView test9(){
		ModelAndView mv = new ModelAndView();
		mv.setViewName("hello");
		mv.addObject("msg", "限定请求参数的映射：参数必须为userId=101");
		return mv;
	}
	@RequestMapping(value="show10",params="userId!=101")
	public ModelAndView test10(){
		ModelAndView mv = new ModelAndView();
		mv.setViewName("hello");
		mv.addObject("msg", "限定请求参数的映射：参数必须为userId!=101");
		return mv;
	}
	@RequestMapping(value="show11",params={"name","userId"})
	public ModelAndView test11(){
		ModelAndView mv = new ModelAndView();
		mv.setViewName("hello");
		mv.addObject("msg", "限定请求参数的映射：参数必须包含name和userId");
		return mv;
	}
	@RequestMapping(value="show12",params={"name=zhangsan","userId!=101"})
	public ModelAndView test12(){
		ModelAndView mv = new ModelAndView("hello");//在构造函数中设置视图名称
		mv.addObject("msg", "限定请求参数的映射：参数必须username为张三并且userId不为101");
		return mv;
	}

	
}
